package com.demo.core;

import java.util.HashSet;
import java.util.Set;

public class ElasticSearchConfig {

	private String clusterName = "";

	private String clients;

	public String getClusterName() {
		return clusterName;
	}

	public void setClusterName(String clusterName) {
		this.clusterName = clusterName;
	}

	public String getClients() {
		return clients;
	}

	public void setClients(String clients) {
		this.clients = clients;
	}

	public Set<Client> getAllClients() {
		Set<Client> set = new HashSet<Client>();
		if(clients!=null && clients.length()>0){
			String[] cs = clients.split(",");
			if(cs!=null && cs.length>0){
				for(String s:cs){
					if(s!=null && s.length()>0){
						String[] c2 = s.split(":");
						if(c2!=null && c2.length==2){
							Client e = new Client();
							if(c2[0]!=null && c2[0].length()>0){
								e.setHost(c2[0].replace(" ", ""));
							}
							try {
								e.setPort(Integer.parseInt(c2[1]));
							} catch (NumberFormatException e1) {
							}
							set.add(e);
						}
					}
				}
			}
		}
		
		return set;
	}

	class Client {
		private String host;
		private int port = 9200;

		public String getHost() {
			return host;
		}

		public void setHost(String host) {
			this.host = host;
		}

		public int getPort() {
			return port;
		}

		public void setPort(int port) {
			this.port = port;
		}
		
		@Override
		public boolean equals(Object obj) {
			Client c1 = (Client) obj;
			if(c1.getHost()!=null && c1.getHost().equals(this.host) && c1.getPort()==this.port){
				return true;
			}else{
				return false;
			}
		}
		
		@Override
		public int hashCode() {
			//return super.hashCode();
			return this.host.hashCode()+this.port;
		}
	}
}
